<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能数据洞察与问卷管理平台</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #1890ff;
            --success-color: #52c41a;
            --error-color: #ff4d4f;
            --text-dark: #1f2d3d;
            --text-light: #5e6c84;
            --border-color: #e4e7ed;
            --shadow-default: 0 4px 12px rgba(0,0,0,0.08);
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f7fa;
            line-height: 1.5;
            margin: 0;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 24px;
        }

        h1 {
            color: var(--text-dark);
            font-size: 24px;
            margin: 0;
            font-weight: 600;
        }

        .nav-user-bar {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-info {
            color: var(--text-dark);
            font-size: 16px;
            font-weight: 500;
            padding: 8px 16px;
            background: white;
            border-radius: 4px;
            box-shadow: var(--shadow-default);
        }

        .question-card {
            background: white;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            padding: 16px;
            margin-bottom: 16px;
            transition: all 0.2s;
        }

        .question-card:hover {
            border-color: var(--primary-color);
            box-shadow: var(--shadow-default);
        }

        .ant-btn {
            padding: 8px 16px;
            background: var(--primary-color);
            border: none;
            color: white;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
            text-decoration: none;
        }

        .ant-btn:hover {
            background: #40a9ff;
            box-shadow: var(--shadow-default);
            transform: translateY(-1px);
        }

        .delete-btn {
            padding: 8px 16px;
            background: var(--error-color);
            border: none;
            color: white;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .delete-btn:hover {
            background: #c5221f;
            box-shadow: var(--shadow-default);
            transform: translateY(-1px);
        }

        .export-btn {
            padding: 8px 16px;
            background: var(--success-color);
            border: none;
            color: white;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
            text-decoration: none;
        }

        .export-btn:hover {
            background: #43b02a;
            box-shadow: var(--shadow-default);
            transform: translateY(-1px);
        }

        .status-info {
            display: flex;
            gap: 15px;
            align-items: center;
            margin: 10px 0;
        }

        .badge {
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 0.9em;
            background: var(--success-color);
            color: white;
        }

        .text-muted {
            color: var(--text-light);
        }

        .action-buttons {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
        }

        .created-by {
            margin: 10px 0;
            padding: 8px 0;
            border-top: 1px solid var(--border-color);
        }

        .filter-form {
            margin-bottom: 20px;
            display: flex;
            gap: 15px;
            align-items: center;
            flex-wrap: wrap;
        }

        .filter-form label {
            font-weight: 500;
            color: var(--text-dark);
        }

        .filter-form select, .filter-form input {
            padding: 8px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            background: white;
            cursor: pointer;
        }

        .filter-form select:focus, .filter-form input:focus {
            outline: none;
            border-color: var(--primary-color);
        }

        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }

            .nav-user-bar {
                flex-direction: column;
                gap: 10px;
            }

            .question-card {
                padding: 12px;
            }

            .status-info {
                flex-direction: column;
            }

            .action-buttons {
                flex-direction: column;
                gap: 8px;
            }

            .created-by {
                border-top: none;
                margin-top: 0;
            }

            .filter-form {
                flex-direction: column;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
        <div class="container">
            <div class="header">
                <h1>问卷列表</h1>
                <div class="nav-user-bar">
                    <span class="user-info">
                        <i class="fas fa-user"></i> 当前用户：{{ request.user.username }}
                    </span>
                    <a href="{% url 'survey_create' %}" class="ant-btn">
                        <i class="fas fa-plus"></i> 创建新问卷
                    </a>
                    <a href="/" class="ant-btn">
                        <i class="fas fa-home"></i> 返回主页
                    </a>
                </div>
            </div>

            <!-- 添加筛选表单 -->
            <form class="filter-form" method="GET" action="{% url 'survey_list' %}">
                <label for="name">问卷名称：</label>
                <input type="text" name="name" id="name" value="{{ name_filter }}" placeholder="输入问卷名称">

                <label for="creator">创建者：</label>
                <input type="text" name="creator" id="creator" value="{{ creator_filter }}" placeholder="输入创建者用户名">

                <label for="order">排序方式：</label>
                <select name="order" id="order" onchange="this.form.submit()">
                    <option value="created_at_desc" {% if order == 'created_at_desc' %}selected{% endif %}>创建时间（降序）</option>
                    <option value="created_at_asc" {% if order == 'created_at_asc' %}selected{% endif %}>创建时间（升序）</option>
                </select>
            </form>

            {% for survey in surveys %}
            <div class="question-card">
                <h5 class="card-title">{{ survey.name }}</h5>
                <div class="status-info">
                    <span class="badge">{{ survey.get_status_display }}</span>
                    <span class="text-muted">
                        创建时间：{{ survey.created_at|date:"Y-m-d H:i" }}
                    </span>
                </div>
                <div class="created-by">
                    <span class="text-muted">创建者：{{ survey.created_by.username }}</span>
                </div>
                <div class="action-buttons">
                    <a href="{% url 'view_survey' survey.id %}" class="ant-btn">
                        <i class="fas fa-eye"></i> 查看问卷
                    </a>
                    <a href="{% url 'export_survey' survey.id %}" class="export-btn">
                        <i class="fas fa-download"></i> 导出问卷
                    </a>
                    <button class="delete-btn" onclick="confirmDelete({{survey.id}})">
                        <i class="fas fa-trash"></i> 删除
                    </button>
                </div>
            </div>
            {% empty %}
            <div class="alert alert-info">
                暂无问卷，点击上方按钮创建新问卷。
            </div>
            {% endfor %}
        </div>

        <script>
            function confirmDelete(surveyId) {
                if (confirm('确定要删除这个问卷吗？')) {
                    fetch(`/delete/${surveyId}/`, {
                        method: 'POST',
                        headers: {
                            'X-CSRFToken': '{{ csrf_token }}'
                        }
                    })
                    .then(response => {
                        if (response.ok) {
                            window.location.reload();
                        } else {
                            alert('删除失败，请重试。');
                        }
                    })
                    .catch(error => {
                        console.error('错误:', error);
                        alert('删除失败，请重试。');
                    });
                }
            }
        </script>
    </body>
</html>